#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@file:getData.py
@time:2017/7/5 0005 11:44
"""
# from ..models import hostGroup,hostInfo,hostDetail
# from fabric.api import *
import paramiko
def basicData(host,user,passwd,port):
    port = int(port)
    data={}
    s = paramiko.SSHClient()
    s.load_system_host_keys()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(host,port,user,passwd,timeout=5)
    getmemcmd = "cat /proc/meminfo| grep MemTotal |awk '{print $2}'"
    getcpucmd = "cat /proc/cpuinfo| grep 'cpu cores'| uniq  |awk '{print $NF}'"
    getdisk = "fdisk -l  | grep 'Disk /dev/'|grep -v mapper  | awk '{print $3}'|awk '{for (i=1;i<=NR;i++)total=total+$i};END {print total}'"
    getprivateip = "python -c 'import socket;print socket.gethostbyname(socket.gethostname())'"
    stdin,stdout,stderr = s.exec_command(getmemcmd)
    mem = stdout.read().decode(encoding='utf-8').strip()
    data['mem']=mem
    stdin,stdout,stderr = s.exec_command(getcpucmd)
    cpu = stdout.read().decode(encoding='utf-8').strip()
    data['cpu']=cpu
    stdin,stdout,stderr = s.exec_command(getdisk)
    disk = stdout.read().decode(encoding='utf-8').strip()
    data['disk']=disk
    stdin,stdout,stderr = s.exec_command(getprivateip)
    privateip = stdout.read().decode(encoding='utf-8').strip()
    data['privateip'] = privateip
    return data
